package com.yunkaweilai.android.view.dialog.login;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.Web;
import com.yunkaweilai.android.model.CodeMsgBean;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.CountDownTimerUtils;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

/**
 * Created by Administrator on 2017/7/24.
 */

public class ForgetCodeDialog extends Dialog {
    private ImageView idImgCode;
    private EditText idEdtCode;
    private TextView idTvClose;    private TextView idTvRight;
    private String cToken = "";
    private String userName;
    private String userTel;
    private Context mContext;
    private CountDownTimerUtils countDownTimerUtils;
    private String isForget;
    private com.yunkaweilai.android.interfaces.DialogMemberPayInterface DialogMemberPayInterface;

    public ForgetCodeDialog(Context context, String userName, String userTel, CountDownTimerUtils countDownTimerUtils, com.yunkaweilai.android.interfaces.DialogMemberPayInterface DialogMemberPayInterface) {
        super(context);
        this.mContext = context;
        this.userName = userName;
        this.userTel = userTel;
        this.countDownTimerUtils = countDownTimerUtils;
        this.DialogMemberPayInterface = DialogMemberPayInterface;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_forger_code);
        idImgCode = ((ImageView) findViewById(R.id.id_img_code));
        idEdtCode = ((EditText) findViewById(R.id.id_edt_code));
        idTvClose = ((TextView) findViewById(R.id.id_tv_close));
        idTvRight = ((TextView) findViewById(R.id.id_tv_right));

        Utils.autoShowKeyboard(idEdtCode);//弹出软键盘
        cToken = Math.random() + "";

        GlideUtils.loadImages(mContext, Web.VERIFICATION_CODE + "?Ctoken=" + cToken,idImgCode,R.mipmap.ic_forget_code_error);
        idImgCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cToken = Math.random() + "";
                GlideUtils.loadImages(mContext, Web.VERIFICATION_CODE + "?Ctoken=" + cToken,idImgCode,R.mipmap.ic_forget_code_error);
            }
        });
        idTvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(idEdtCode.getText().toString())){
                    initHttp();
                }else{
                    MyToast.showShortToast(mContext,"还未输入验证码");
                }
            }
        });
    }

    private void initHttp(){
        HttpRequest.postUrl(Api.GET_SMS_CODE)
                .addParams("Phone",userTel)
                .addParams("UserName",userName)
                .addParams("Ctoken",cToken+"")
                .addParams("ImgCode",idEdtCode.getText().toString())
                .addParams("isForget","1")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mContext, json,"短信");
                        if (checkCode){
                            countDownTimerUtils.start();
                            DialogMemberPayInterface.passPay(cToken);
                            dismiss();
                        }
                        else {
                            Gson gson = new Gson();
                            CodeMsgBean codeMsgBean = gson.fromJson(json, CodeMsgBean.class);
//                            MyToast.showShortToast(mContext,codeMsgBean.getMessage());
                            MyToast.showShortToast(mContext,"验证码错误");
                            Log.d("xxx", "onResponse: 1");

                        }
                    }

                    @Override
                    public void onError(Exception e) {
//                        MyToast.showShortToast(mContext,"验证码错误");
                        MyToast.showShortToast(mContext,e.getMessage());
                        Log.d("xxx", "onResponse: 2");
                    }
                });
    }


}
